/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package CLIENT;

import java.io.IOException;
import java.io.ObjectInputStream;
import java.net.UnknownHostException;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 * This class listenes to new messages from the server and sends them to the client
 * (A Thread Class)
 * @author Yaniv Kalif
 */
public class ClientMessageListener implements Runnable
{
    ClientConnection _connection;
    ObjectInputStream in = null;
    ClientMainChatPanel _CreatorP;
    Client _client;
    /**
     * constructor for This class
     * sets the connection and the creator fields
     * @param connection
     * @param Creator
     * @throws IOException
     */
    public ClientMessageListener(ClientConnection connection,ClientMainChatPanel Creator) throws IOException
    {
        _connection = connection;
        _CreatorP=Creator;
    }
    /**
     * This function runs the Thread
     * handles all The Messages recieved for the Client
     */
    public void run()
    {
        try 
        {
            in = new ObjectInputStream(_connection.GetConnectedSocket().getInputStream());
            String msg = null;
            Message m = null;
            System.out.println("client Accept");

            while ((m = (Message) in.readObject()) != null)
            {

                if(in != null)
                {
                    System.out.println("server:" + m);
                    if (m.GetisSystemMessage())
                    {
                        if (m.GetString().startsWith("@"))
                        {
                            String message = m.GetString().substring(1);
                            String[] usersList = message.split(";");

                            _CreatorP.ClearUsersList();

                            for (int j = 0; j < usersList.length; j++)
                            {
                                _CreatorP.AddUserToList(usersList[j]);
                            }
                        }
                        else
                        {
                            _client=_CreatorP.GetClient();
                            _client.SetID(Integer.valueOf(m.GetString()));
                            _connection.SendID(_client, String.valueOf(_client.GetName())); //send this name to the server
                        }
                    }
                    else
                        _CreatorP.RecieveMessage(m);// send the message to all the users
                }
            }
        }
        catch (UnknownHostException ex) {
            Logger.getLogger(ClientMessageListener.class.getName()).log(Level.SEVERE, null, ex);
        } catch (InterruptedException ex) {
            Logger.getLogger(ClientMessageListener.class.getName()).log(Level.SEVERE, null, ex);
        }        catch (ClassNotFoundException ex)
        {
            Logger.getLogger(ClientMessageListener.class.getName()).log(Level.SEVERE, null, ex);
        }
        catch (IOException ex)
        {
            Logger.getLogger(ClientMessageListener.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

}
